package com.voice.call.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.FeedBack;
import com.voice.call.service.FeedBackService;
import com.voice.call.version.ApiVersion;

//用户反馈
@RestController
@RequestMapping("/api/{version}/feedback/")
public class FeedBackController {
	
	private final Logger log = LoggerFactory.getLogger(FeedBackController.class);
	
	@Resource
	private FeedBackService feedBackService;
	
	@Resource
	private Properties configProperties;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "suggestion", method = RequestMethod.POST) //produces = Const.JSON_UTF8
	public Result feedbackSuggestion(HttpServletRequest request,@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
		
		 String lang = Const.LANGUAGE;
	     if (request.getParameter("lang")!=null) {
	    	   
	    	   if(ConstLang.langArr.contains(request.getParameter("lang").toString())){
	    		   lang = request.getParameter("lang");
	    	   }
			
		 }
		
		if(request.getParameter("email")==null || request.getParameter("content")==null){
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		FeedBack entity=new FeedBack();
		entity.setEmail(request.getParameter("email").toString());
		entity.setContent(request.getParameter("content").toString());
		entity.setCtime(new Date());
		if(file!=null){
			
			String imgUrl = configProperties.getProperty(Const.UPLOAD_HTTP)+"feedback/"+file.getOriginalFilename();
			String path = configProperties.getProperty(Const.UPLOAD_SAVE_PATH)+"feedback/";
			try {
				
				File targetFile = new File(path, file.getOriginalFilename());
				//判断文件夹是否已经存在，如果已经存在了重新建   
				if (!targetFile.exists()) {   
				    targetFile.mkdirs();   
				}
				//转存文件
				file.transferTo(targetFile);
				
				entity.setImagePic(imgUrl);
				
			} catch (Exception e) {
				e.printStackTrace();
				//log.error("保存文件失败");
			}
			
			
		}
		
		if(feedBackService.addFeedBack(entity)){
			return new Result(ResultEnum.SUCCESS,lang);
		}
		return new Result(ResultEnum.FAILED,lang);
	
		
		/**
		System.out.println("文件长度: " + file.getSize());
		System.out.println("文件类型: " + file.getContentType());
		System.out.println("文件名称: " + file.getName());//<input type="file" name="file" />   
		System.out.println("文件原名: " + file.getOriginalFilename());
		System.out.println("========================================");
		**/
		
		//MultipartFile file = (MultipartFile)request.getParameter("file");
		//System.out.println("password:"+password);
		//System.out.println("email:"+email);
		//如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解
		//如果想上传多个文件，那么 这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解
		//并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
		/***
		for(MultipartFile myfile : files){
			if(myfile.isEmpty()){
				System.out.println("文件未上传");
			}else{
				System.out.println("文件长度: " + myfile.getSize());
				System.out.println("文件类型: " + myfile.getContentType());
				System.out.println("文件名称: " + myfile.getName());
				System.out.println("文件原名: " + myfile.getOriginalFilename());
				System.out.println("========================================");
				//如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
				//String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
				//这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
				//FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
			}
		}
		***/

		
		
	}
}
